package com.zl.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zl.dao.AgencyDao;
import com.zl.dao.CollectDao;
import com.zl.entity.Agency;
import com.zl.entity.Collect;
import com.zl.entity.NewsEntity;
import com.zl.entity.QueryEntity;
import com.zl.util.SearchUtils;

@Service
public class CollectService {
	@Autowired
	private CollectDao collectDao;
	@Autowired
	private AgencyDao agencyDao;
	/**
	 * 增加收藏
	 * @param collect
	 */
	public void addCollect(Collect collect){
		collectDao.addCollect(collect);
	}
	/**
	 * 判断是否收藏
	 * @param userId
	 * @param collectId
	 * @return
	 */
	public List<Collect> isExist(String userId,String collectId){
		Collect collect = new Collect();
		collect.setUserId(userId);
		collect.setCollectId(collectId);
		return collectDao.isExist(collect);
	}
	
	public void cancelCollect(String id){
		collectDao.cancelCollect(id);
	}
	
	public List<NewsEntity> getCollectNews(String userid){
		Collect queryCollect = new Collect();
		queryCollect.setUserId(userid);
		queryCollect.setType("0");
		List<Collect> list = collectDao.getCollectList(queryCollect);
		List<NewsEntity> newslist = new ArrayList<NewsEntity>();
		for(Collect collect:list){
			String collectId = collect.getCollectId();
			QueryEntity queryEntity = new QueryEntity();
			queryEntity.setId(collectId);
			List<NewsEntity> news = SearchUtils.search(queryEntity);
			if(news.size()>0){
				newslist.add(news.get(0));
			}
		}
		return newslist;
	}
	
	public List<Agency> getCollectAgency(String userid){
		Collect queryCollect = new Collect();
		queryCollect.setUserId(userid);
		queryCollect.setType("1");
		List<Collect> list = collectDao.getCollectList(queryCollect);
		List<Agency> agencylist = new ArrayList<Agency>();
		for(Collect collect:list){
			String collectId = collect.getCollectId();
			Agency agency = agencyDao.getAgencyById(collectId);
			if(agency!=null){
				agencylist.add(agency);
			}
		}	
		return agencylist;
	}
}
